// 用户内容
let ouserLogin = document.querySelector
    ("#user-login");
// 邮箱后缀提示框
let osugLogin = document.querySelector(".user-sug");
// 用户名后面叉
let ocloseUser = document.querySelector(".close-user");
// 密码后面叉
let oclosePsd = document.querySelector(".close-psd");
// 密码内容
let opsdLogin = document.querySelector("#password-login")
// 验证码盒子
let overify = document.getElementById("verify");

// 登录按钮
let osubmit = document.querySelector("#submit");
// 提示框内容
let otipLogin = document.querySelector(".tip-login");
// 获取验证码按钮
let osend = document.querySelector("#send");


// 点击叉叉清空内容及提示
ocloseUser.onclick = function () {
    ouserLogin.value = null;
    // osugLogin.style.display = "none";
    opsdLogin.value = null;
    otipLogin.innerHTML = null;
}
oclosePsd.onclick = function () {
    opsdLogin.value = null;
    otipLogin.innerHTML = null;
}


// input内容为空时，清空提示
ouserLogin.addEventListener("input", () => {
    let str = ouserLogin.value;
    if (str.length === 0) {
        otipLogin.innerHTML = null;
    }
})
ouserLogin.addEventListener("focus",() =>{
    otipLogin.innerHTML = null;
})
opsdLogin.addEventListener("focus",() =>{
    otipLogin.innerHTML = null;
})


// 邮箱提示后缀
let loginList = ["163.com"];
let nowSelectTipIndex = 0;
ouserLogin.oninput = function (e) {
    judge();
    add();
}
ouserLogin.addEventListener("input", (e) => {
    judge();
    add();
})
osugLogin.addEventListener("click", function (eve) {
    var e = eve || window.event;
    let target = eve.target || eve.srcElement;
    if (target.nodeName.toLowerCase() == "li") {
        hide();
        ouserLogin.focus();
        return ouserLogin.value = htmlDecode(target.innerHTML);
    }
})

function getText() {
    let inputText = ouserLogin.value/* .trim() */;
    return inputText;
}
function postlist() {
    let userinput = getText();
    let newloginList = new Array();
    if (userinput.search('@') != 0) {
        let len = userinput.search('@');
        let x = userinput.substring(len + 1, userinput.length);
        for (let i = 0; i < loginList.length; i++) {
            if (loginList[i].search(x) == 0) {
                newloginList.push(loginList[i]);
            }
        }
        if (x === '' || newloginList == '') {
            return loginList;
        }
        return newloginList;
    } else {
        return loginList;
    }
}
function promptContent() {
    let x = htmlEncode(getText());
    let tips = new Array();
    if (x.indexOf("@") != -1) {
        let p = x.slice(0, x.indexOf("@"));
        for (i = 0; i < postlist().length; i++) {
            tips[i] = p + "@" + postlist()[i];
        }
    } else {
        for (i = 0; i < loginList.length; i++) {
            tips[i] = x + "@" + loginList[i];
        }
    }
    return tips;
}
function add() {
    // let sug = document.getElementById("email-sug-wrapper");
    let tips = promptContent();
    while (osugLogin.hasChildNodes()) {
        osugLogin.removeChild(osugLogin.firstChild);
    }
    for (i = 0; i < tips.length; i++) {
        let tip_li = document.createElement("li");
        tip_li.innerHTML = tips[i];
        osugLogin.appendChild(tip_li);
    }
    let list = document.getElementsByTagName("li");
    list[0].setAttribute("class", "active");
}

function judge() {
    if (getText() == "") {
        hide();
    } else {
        display();
    }

}
function hide() {
    osugLogin.style.display = "none";
}
function display() {
    osugLogin.style.display = "block";
}
function htmlEncode(html) {
    let temp = document.createElement("div");
    (temp.textContent != undefined) ? (temp.textContent = html) : (temp.innerText = html);
    let output = temp.innerHTML;
    temp = null;
    return output;
}
function htmlDecode(text) {
    let temp = document.createElement("div");
    temp.innerHTML = text;
    let output = temp.innerText || temp.textContent;
    temp = null;
    return output;
}


// // foucs时添加盒子边框阴影
ouserLogin.addEventListener("click", () => {
    ouserLogin.style.boxShadow = " 1px 1px 1px #2c82ff, -1px -1px 1px #2c82ff";
})

ouserLogin.addEventListener("blur", () => {
    ouserLogin.style.boxShadow = "none";
})

opsdLogin.addEventListener("click", () => {
    opsdLogin.style.boxShadow = " 1px 1px 1px #2c82ff, -1px -1px 1px #2c82ff";
})

opsdLogin.addEventListener("blur", () => {
    opsdLogin.style.boxShadow = "none";
})


overify.addEventListener("focus", () => {
    overify.style.boxShadow = " 1px 1px 1px #2c82ff, -1px -1px 1px #2c82ff";
})
overify.addEventListener("blur", () => {
    overify.style.boxShadow = "none";
})


osubmit.addEventListener("click", reload);
function reload() {
    ajax({
        type: "get",
        url: "http://localhost:3000/login",
        data: {
            type: "login",
            username: ouserLogin.value,
            password: opsdLogin.value,
        },
        success: (res) => {
            console.log(res);
            res = JSON.parse(res);
            console.log(res);
            // console.log((arr[0]));

            if (res.code === 0) {
                otipLogin.innerHTML = "用户名不存在"
            } else if (res.code === 2) {
                otipLogin.innerHTML = "密码错误"
            } else if (res.code === 1) {
                if (overify.value === arr[0]) {

                    localStorage.setItem("state", "ok");
                    localStorage.setItem("userid", res.p);

                    if (confirm("登录成功，是否跳转到首页")) {
                        location.href = "https://www.baidu.com/";
                    }
                } else {
                    otipLogin.innerHTML = "验证码输入有误"
                }
            }
        }

    })
}

// 点击验证码发送邮箱验证码
osend.addEventListener("click", function () {
    if (ouserLogin.value !== "" && opsdLogin.value !== "") {
        this.setAttribute("disabled", "disabled")
        let sec = 60;
        let that = this
        let t = setInterval(function () {
            sec = sec - 1;
            if (sec > 0) {
                that.value = `重新获取${sec}s`;
            } else {
                clearInterval(t);
                that.value = `发送验证码`;
                that.removeAttribute("disabled")
            }
        }, 1000)
    }else{
        osend.value = "发送验证码";
        osend.setAttribute("disabled", "disabled");

    }

})

osend.addEventListener("click", function () {
    if (ouserLogin.value !== "" && opsdLogin.value !== "") {
        Verify()
        alert("验证码发送成功，请查看邮箱")
    } else {
        osend.setAttribute("disabled", "disabled");
        otipLogin.innerHTML = "请输入账号密码";
        console.log(789);
    }
});
let arr = []
function Verify() {
    ajax({
        type: "get",
        url: "http://localhost:3000/getEmail",
        success: (response) => {
            return arr.unshift(response);
        },

    })
}
console.log(arr);

